package com.coder.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coder.entity.CollSat;
import com.coder.entity.MajorSat;
import com.coder.mapper.MajorSatMapper;
import com.coder.service.MajorSatService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王令
 */
@Service
public class MajorSatServiceImpl extends ServiceImpl<MajorSatMapper, MajorSat> implements MajorSatService {
    @Override
    public Map<String, Object> majorSatInfo() {
        Map<String, Object> map = new HashMap<>();
        List<MajorSat> collect = list(new QueryWrapper<MajorSat>().select("distinct major"));
        List<String> colleges = new ArrayList<>();
        collect.forEach(v -> {
            colleges.add(v.getMajor());
        });
        map.put("major", colleges);
        return map;
    }

    @Override
    public IPage<MajorSat> pages(Page<MajorSat> page, MajorSat majorSat) {
        return page(page, new QueryWrapper<MajorSat>()
                .select("colleges", "level", "major", "major_sat")
                .like(StrUtil.isNotBlank(majorSat.getColleges()), "colleges", majorSat.getColleges())
                .like(StrUtil.isNotBlank(majorSat.getMajor()), "major", majorSat.getMajor())
                .orderBy(StrUtil.isNotBlank(majorSat.getOrderProp()) && StrUtil.isNotBlank(majorSat.getOrder()),
                        StrUtil.equals("ascending", majorSat.getOrder()),
                        StrUtil.isNotBlank(majorSat.getOrderProp())? majorSat.getOrderProp().replace("S", "_s"): "major_sat")
        );
    }
}
